package org.dreamfly.core.util;


import org.dreamfly.core.bean.RemoteServiceInfo;
import org.dreamfly.core.bean.ServerIpInfo;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by IntelliJ IDEA.
 * User: Gin
 * Date: 2010-11-27
 * Time: 0:14:04
 * To change this template use File | Settings | File Templates.
 */
public class XMLReader {
    
     public static List<RemoteServiceInfo> readServerInfo(String file){
         List<RemoteServiceInfo> serviceInfoList = new ArrayList<RemoteServiceInfo>();

         SAXBuilder builder = new SAXBuilder();
         try {
             Document document = builder.build(new File(file));
             Element foo = document.getRootElement();
             List childrenList = foo.getChildren();
             for(int i = 0 ; i < childrenList.size() ; i++){
                 RemoteServiceInfo serviceInfo = new RemoteServiceInfo();
                 serviceInfo.setServerClass(((Element)childrenList.get(i)).getChild("class").getText());
                 serviceInfo.setServerId(((Element)childrenList.get(i)).getChild("id").getText());
                 serviceInfo.setServerType(((Element)childrenList.get(i)).getChild("type").getText());
                 serviceInfo.setServerUrl(((Element)childrenList.get(i)).getChild("url").getText());
                 serviceInfoList.add(serviceInfo);
             }
         } catch (JDOMException e) {
             e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
         } catch (IOException e) {
             e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
         }
         return serviceInfoList;
     }

    public static Map<String,Object> readServerIpInfo(String file){
        Map<String,Object> map = new HashMap<String, Object>();
        SAXBuilder builder = new SAXBuilder();
         try {
             Document document = builder.build(new File(file));
             Element foo = document.getRootElement();
             List childrenList = foo.getChildren();
             for(int i = 0 ; i < childrenList.size() ; i++){
                 String key = ((Element)childrenList.get(i)).getChild("serverName").getText();
                 ServerIpInfo serverIpInfo = new ServerIpInfo();
                 serverIpInfo.setIp(((Element)childrenList.get(i)).getChild("ip").getText());
                 Integer port = new Integer(((Element)childrenList.get(i)).getChild("port").getText());
                 serverIpInfo.setPort(port);

                 map.put(key,serverIpInfo);
             }
         } catch (JDOMException e) {
             e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
         } catch (IOException e) {
             e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
         }
        return map;
    }
}
